//
//  InterstitialVC.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "InterstitialVC.h"
#import <CJMobileAd/CJMobileAd.h>
#import "CustomShowView.h"
#import <AnyThinkInterstitial/AnyThinkInterstitial.h>

@interface InterstitialVC ()<ATInterstitialDelegate>

@property (nonatomic, strong) CJInterstitialAd *interstitialAd;

@end

@implementation InterstitialVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)loadAdData {
    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65eac4cfd7aa0" extra:@{} delegate:self];
}

- (void)setupUI {
    self.title = @"InterstitialAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    if ([[ATAdManager sharedManager] interstitialReadyForPlacementID:@"b65eac4cfd7aa0"]) {
        // 广告已填充，展示广告
        [[ATAdManager sharedManager] showInterstitialWithPlacementID:@"b65eac4cfd7aa0" inViewController:self delegate:self];
    } else {
        // 广告未填充，是否重新加载
    }
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak InterstitialVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}


#pragma mark CJInterstitialAdDelegate


/// 广告位加载成功
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    NSLog(@"事件:%s", __FUNCTION__);
    [self.customView canShowStatus:true errorMsg:@""];
}

/// Callback of ad loading failure
/// 广告位加载失败
- (void)didFailToLoadADWithPlacementID:(NSString*)placementID
                                 error:(NSError*)error {
    NSLog(@"事件:%s", __FUNCTION__);
    [self.customView canShowStatus:false errorMsg:@""];
}
                                 
/// Interstitial ad displayed successfully
/// 插屏广告展示成功
- (void)interstitialDidShowForPlacementID:(NSString *)placementID
                                    extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Interstitial ad clicked
/// 插屏广告被点击了
- (void)interstitialDidClickForPlacementID:(NSString *)placementID
                                     extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
    NSLog(@"---插屏被点击了---");
}

/// Interstitial ad closed
/// 插屏广告被关闭了
- (void)interstitialDidCloseForPlacementID:(NSString *)placementID
                                     extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
    [self.customView canShowStatus:false errorMsg:@""];
}

@end
